package com.chatgpt_backend.controller;

import com.chatgpt_backend.entity.RespBean;
import com.chatgpt_backend.entity.user.RegisterUser;
import com.chatgpt_backend.service.AuthorizeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import jakarta.websocket.server.PathParam;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@RequestMapping("/api/auth")
public class AuthorizeController {

    private final String EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$";

    @Resource
    AuthorizeService authorizeService;

    @RequestMapping("/valid-register-email")
    public RespBean<String> validRegisterEmail(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email,
                                       HttpSession session) {
        String s =authorizeService.sendValidEmail(email, false, session.getId());
        if (s == null)
            return RespBean.success("邮件以发送，请注意查收");
        else
            return RespBean.failure(400, s);
    }

    @RequestMapping("/valid-reset-email")
    public RespBean<String> validResetEmail(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email,
                                       HttpSession session) {
        String s =authorizeService.sendValidEmail(email, true, session.getId());
        if (s == null)
            return RespBean.success("邮件以发送，请注意查收");
        else
            return RespBean.failure(400, s);
    }

    @RequestMapping("/register")
    public RespBean<String> register(@Valid @RequestBody RegisterUser user,
                                     HttpSession session) {
        String s = authorizeService.validateRegisterAccount(user, session.getId());
        if (s == null) return RespBean.success("注册成功, 即将为您跳转");
        else return RespBean.failure(401, s);
    }

    @PostMapping("/start-reset")
    public RespBean<String> reset(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email,
                                  @Length(min = 6, max = 6) @RequestParam("code") String code,
                                  HttpSession session) {
        String s = authorizeService.validateOnly(email, code, session.getId());
        if (s==null) {
            session.setAttribute("email", email);
            return RespBean.success("验证通过");
        }
        else return RespBean.failure(401, s);
    }

    @PostMapping("/do-reset")
    public RespBean<String> doReset(@Length(min = 6, max = 16) @RequestParam("password") String newPassword,
                                    HttpSession session) {
        String email = (String) session.getAttribute("email");
        if (email==null) return RespBean.failure(401, "请先完成邮箱验证");
        else if (authorizeService.resetPsd(email, newPassword)){
            session.removeAttribute("email");
            return RespBean.success("重置密码成功");
        }else return RespBean.failure(401, "服务器错误, 请联系管理员");
    }
}
